/****************************************
* James Gomez                           *
* Project 3: Target Practice P3         *
* Comp 465                              *
* Professor Barnes                      *
* 12/7/2012                             *
*****************************************/

#ifndef _PLANET_
#define _PLANET_

#include "GomezJamesP3_OpenGL.h"
#include "GomezJamesP3_CollidableObject.cpp"
#include "GomezJamesP3_Movable.cpp"

#include "GomezJamesP3_Moon.cpp"
#include "GomezJamesP3_MissileBattery.cpp"

#include <string>
#include <vector>
using namespace std;


/****************************************************************************
Movable, Collidable object that orbits around a sun. May have 0 or more moons 
that orbit around it, as well as 0 to 1 missile batteries 
*****************************************************************************/
class Planet: public CollidableObject, Movable{

private:
	Object3D * anchor;
	GLdouble radius;	
	GLfloat angle, angleIncrement;
	Color color[3];
	GLfloat  offset[3];

public:
	Planet(const string name, const GLdouble r, const Color c[3], Object3D * anchor, 
		const GLfloat startLoc[3], const GLfloat angleInc);
	~Planet(){}

	void draw();
	void move();
	
};


Planet::Planet(const string name, const GLdouble r, const Color c[3], Object3D * anchor, 
		const GLfloat startLoc[3], const GLfloat angleInc): CollidableObject(name, r){

	this->anchor = anchor;
	this->radius = r;
	this->angleIncrement = angleInc;
	this->angle = 0;
		
	for (int i=0; i<3; i++){
		this->color[i] = c[i];
		offset[i] = startLoc[i];
	}	

	//glMatrixMode(GL_MODELVIEW);
	////save to OM
	//glPushMatrix();
	//glLoadIdentity();
	//glRotatef(angleInc, 0, 1, 0);
	//glTranslatef(offset[0], offset[1], offset[2]);
	//glGetFloatv(GL_MODELVIEW_MATRIX, OM);
	//glPopMatrix();
}

void Planet::draw(){	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();	
	glMultMatrixf(TM);
	//drawAxes(1000);
	glColor3fv(color);
	glutSolidSphere(radius, SPHERE_SLICES, SPHERE_STACKS);	
	glPopMatrix();	
}

void Planet::move(){
	
	angle += angleIncrement;
	if (angle >= 360) angle = 0;

	glMatrixMode(GL_MODELVIEW);	
	glPushMatrix();
	resetTM();
	GLfloat m[16];
	anchor->getTM(m);
	glLoadMatrixf(m);
	glRotatef(angle, 0, 1, 0);
	glTranslatef(offset[0], offset[1], offset[2]);
	glGetFloatv(GL_MODELVIEW_MATRIX, TM);	
	glPopMatrix();
}

#endif